﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Seesmic.Sdp.Extensibility;
using System.Threading;
using Seesmic.Sdp.Utils;
using PTiRouZ.Net.SilverLight.Api.HelloTipi;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Globalization;

namespace Seesmic.HelloTipi
{
    public class HelloTipiUserTimeLineItemsProvider : ITimelineItemsProvider
    {

        #region Fields

        private Timer _Timer;

        private int messageId = 0;

        private HelloTipiAccount _account;

        public apitxt api;

        #endregion Fields

        #region Properties

        public HelloTipiTimeLine Timeline
        {
            get;
            private set;
        }

        #endregion Properties

        #region ITimelineItemsProvider Members

        public event EventHandler<TimelineItemEventArgs> ItemAdded;

        public event EventHandler<TimelineItemEventArgs> ItemRemoved;

        #region Constructors

        public HelloTipiUserTimeLineItemsProvider(HelloTipiAccount Account)
        {
            _account = Account;

            this.Timeline = new HelloTipiTimeLine();

            this.api = new apitxt(Account.Username, Account.Password, "Seesmic Desktop 2");

            this.api.APICallError += delegate(object sender, ApiErrorEventArgs e)
            {
                HelloTipiPlugin.ShowDialogNotification("Erreur de l'api", "{0}", DialogNotificationButtons.OK, e.Message);
            };

            this.api.UserTimelineCompleted += new UserTimelineEventHandler(api_UserTimelineCompleted);

            _Timer = new Timer(OnTimerTick, null, 0, 30000);
        }

        #endregion Constructors

        public void OnItemProcessed(ProcessingResult result)
        {
            if (result.Blocked == false && result.Error == null)
            {
                Timeline.Add(result.TimelineItemContainer);
            }
        }

        private void OnItemAdded(TimelineItemEventArgs e)
        {
            var handler = ItemAdded;

            if (handler != null)
            {
                handler(this, e);
            }            
        }

        private void OnItemRemoved(TimelineItemEventArgs e)
        {
            //HelloTipiPlugin.LogInfo("USer OnItemRemoved " + e.TimelineItemContainer.TimelineItem.Id);

            Timeline.Remove(e.TimelineItemContainer);

            var handler = ItemRemoved;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        private void OnTimerTick(object state)
        {
            if (messageId > 0)
                this.api.UserTimeline(null, messageId);
            else
                this.api.UserTimeline(null, null, 30, null);
        }

        void api_UserTimelineCompleted(object sender, UserTimelineEventArgs e)
        {
            e.result.ForEach(delegate(user_timeline userTimeline)
            {
                HelloTipiTimeLineItem Item = new HelloTipiTimeLineItem
                {
                    Id = userTimeline.id.ToString(),
                    Text = userTimeline.text,
                    DateTime = DateTime.ParseExact(userTimeline.created_at, "ddd MMM d H:mm:ss zzz yyyy", CultureInfo.InvariantCulture),
                    Site = HelloTipiPlugin.getSite(userTimeline.site.name),
                    User = userTimeline.user,
                    Source = new Uri(userTimeline.source),
                    Account = this._account,
                    isFamilyTimeline = false,
                    isPrivateTimeline = false,
                };

                TimelineItemContainer container = new TimelineItemContainer(Item);
                container.AddAction(new DeleteAction(int.Parse(Item.Id), OnItemRemoved));

                container.HighlightColor = ItemHighlightColors.Sent;
                                
                SynchronizationHelper.Post(OnItemAdded, new TimelineItemEventArgs(container));

                if (messageId < userTimeline.id)
                    messageId = userTimeline.id;
            });
        }

        #endregion
    }
}
